package org.codehaus.mojo.wagon;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.util.List;

/**
 * Work around for WAGON-407 to copy commons-io, commons-lang, and jsoup to ${maven.home}/lib/ext directory.
 */
@Mojo(name = "update-maven-3", requiresProject = false)
public class UpdateMaven3Mojo extends AbstractMojo {

    @Component
    private ArtifactResolver artifactResolver;

    @Component
    private ArtifactFactory artifactFactory;

    @Parameter(defaultValue = "${project.remoteArtifactRepositories}")
    private List<ArtifactRepository> remoteRepositories;

    @Parameter(defaultValue = "${localRepository}")
    private ArtifactRepository localRepository;

    /**
     * commons-io:commons-io version
     */
    @Parameter(property = "commonsIoVersion", defaultValue = "2.2")
    private String commonsIoVersion = "2.2";

    /**
     * commons-lang:commons-lang version
     */
    @Parameter(property = "commonsLangVersion", defaultValue = "2.6")
    private String commonsLangVersion = "2.6";

    /**
     * org.jsoup:jsoup version
     */
    @Parameter(property = "jsoupVersion", defaultValue = "1.7.2")
    private String jsoupVersion = "1.7.2";

    @Override
    public void execute() throws MojoExecutionException {
        updateMavenLib(this.artifactFactory.createBuildArtifact("commons-io", "commons-io", commonsIoVersion, "jar"));
        updateMavenLib(this.artifactFactory.createBuildArtifact("commons-lang", "commons-lang", commonsLangVersion, "jar"));
        updateMavenLib(this.artifactFactory.createBuildArtifact("org.jsoup", "jsoup", jsoupVersion, "jar"));
    }

    private void updateMavenLib(Artifact artifact) throws MojoExecutionException {
        try {
            File mavenLibDir = new File(System.getProperty("maven.home"), "lib/ext");
            artifactResolver.resolve(artifact, remoteRepositories, localRepository);
            this.getLog().info("Copy " + artifact.getFile() + " to " + mavenLibDir);
            FileUtils.copyFileToDirectory(artifact.getFile(), mavenLibDir);
        } catch (Exception e) {
            throw new MojoExecutionException("Unable to download artifact", e);
        }
    }
}
